package com.module.app.widget.gallery

import android.content.Context
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.CompositePageTransformer
import androidx.viewpager2.widget.MarginPageTransformer
import androidx.viewpager2.widget.ViewPager2
import com.module.app.widget.gallery.GalleryFragment.Companion.getInstance
import com.module.base.cache.CacheTemporarySDK
import com.module.base.utils.CollectionUtil
import com.module.base.utils.DisplayUtils
import com.module.base.widget.pager.MyFragmentAdapter
import com.module.base.widget.transformer.ScaleInTransformer
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.FullScreenPopupView
import com.module.app.R
import com.module.frame.ext.observe

open class GalleryPop(context: Context) : FullScreenPopupView(context) {

    companion object {
        val KEY_DATA = GalleryPop::class.java.simpleName + "_key_data"

        open fun show(context: Context?, list: List<String>) {
            if (context !is FragmentActivity) {
                return
            }
            CacheTemporarySDK.put(KEY_DATA, list)
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .hasShadowBg(true)
                .asCustom(GalleryPop(context)).show()
        }
    }


    lateinit var viewPager: ViewPager2
    private var defaultPosition = 0
    private val fragmentList: MutableList<Fragment> = ArrayList()
    private lateinit var list: MutableList<String>
    override fun onCreate() {
        super.onCreate()
        viewPager = findViewById(R.id.viewPager)
        initViewPage()
        initListener()
        findViewById<View>(R.id.layout).setOnClickListener {
            onClickLayout()
        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.app_fragment_tab_gallery
    }

    protected fun initListener() {
        observe(lifecycleScope, this, galleryClickFlow) {
            com.module.app.widget.preview.MyPictureExternalPreviewActivity.start(
                context as FragmentActivity, viewPager.currentItem, list
            )
        }
    }

    private fun initViewPage() {
        list =
            CacheTemporarySDK.getTemporary(KEY_DATA, MutableList::class.java) as MutableList<String>
        for (url in list) {
            fragmentList.add(getInstance(url))
        }
        defaultPosition = if (CollectionUtil.getSize(list) > 2) 1 else 0
        //设置画廊
        val transformer = CompositePageTransformer()
        transformer.addTransformer(MarginPageTransformer(DisplayUtils.dip2px(20f)))
        transformer.addTransformer(ScaleInTransformer())
        viewPager.setPageTransformer(transformer)
        //一屏多页
        val recyclerView = viewPager.getChildAt(0)
        if (recyclerView != null && recyclerView is RecyclerView) {
            recyclerView.setPadding(DisplayUtils.dip2px(100f), 0, DisplayUtils.dip2px(100f), 0)
            recyclerView.clipToPadding = false
        }
        val adapter = MyFragmentAdapter((context as FragmentActivity), fragmentList)
        viewPager.adapter = adapter
        viewPager.offscreenPageLimit = fragmentList.size
        viewPager.currentItem = defaultPosition
        viewPager.isUserInputEnabled = true
    }

    override fun dismiss() {
        super.dismiss()
    }

    fun onClickLayout() {
        dismiss()
    }
}